import AdMGBackend from "./AdMGBackend";
import MeiZuADApi from "./MeiZuPlatform/MeiZuADApi";
import TouTiaoADApi from "./TouTiaoADApi/TouTiaoADApi";
import OppoADApi from "./oppoPlatform/OppoADApi";
import { PlatformAdApi } from "./PlatformAdApi";
import QQADApi from "./qqPlatform/QQADApi";
import VivoADApi from "./vivoPlatform/VivoADApi";
import WXADApi from "./wxPlatform/WXADApi";
import BaiDuADApi from "./BaiduPlatform/BaiDuADApi";
import HuaWeiADApi from "./HuaWeiPlatform/HuaWeiADApi";

export enum Platform {
    oppo_H5 = 1,
    vivo_H5,
    qq_H5,
    weixin_H5,
    baidu_H5,
    toutiao_H5,
    meizu_H5,
    huawei_H5
}

const { ccclass, property } = cc._decorator;

@ccclass
export default class AdManager {

    public platform: Platform = Platform.oppo_H5;
    private platformAdApi: PlatformAdApi;
    public adMGBackend: AdMGBackend;

    private static _adManager: AdManager = null;
    public static getInstance(): AdManager {
        if (this._adManager == null) {
            this._adManager = new AdManager();
        }
        return this._adManager;
    }

    public onInit(_callBack: Function) {
        this.adMGBackend = AdMGBackend.getInstance();
        switch (this.platform) {
            case Platform.oppo_H5:
                this.platformAdApi = new OppoADApi();
                break;
            case Platform.vivo_H5:
                this.platformAdApi = new VivoADApi();
                break;
            case Platform.qq_H5:
                this.platformAdApi = new QQADApi();
                break;
            case Platform.weixin_H5:
                this.platformAdApi = new WXADApi();
                break;
            case Platform.meizu_H5:
                this.platformAdApi = new MeiZuADApi();
                break;
            case Platform.toutiao_H5:
                this.platformAdApi = new TouTiaoADApi();
                break;
            case Platform.baidu_H5:
                this.platformAdApi = new BaiDuADApi();
                break;
            case Platform.huawei_H5:
                this.platformAdApi = new HuaWeiADApi();
                break;
        }
        this.adMGBackend.init(this.platform).then(() => {
            this.platformAdApi.onInit(_callBack);
        });

    }

    /**登录 */
    public onLogin() {
        this.platformAdApi && this.platformAdApi.onLogin();
    }
    /**分享游戏链接 */
    public onShare(_callback: Function) {
        this.platformAdApi && this.platformAdApi.onShare(_callback);
    }
    /**创建banner */
    public createBanner() {
        this.platformAdApi && this.platformAdApi.createBanner();
    }
    /**展示banner */
    public showBanner(adIdKey, openIdKey?: string) {
        if (!this.adMGBackend.adShow(openIdKey)) {
            return;
        }
        this.platformAdApi && this.platformAdApi.showBanner(adIdKey, openIdKey);
    }
    /**隐藏banner */
    public hideBanner() {
        this.platformAdApi && this.platformAdApi.hideBanner();
    }
    /**创建激励视频 */
    public createVideo() {
        this.platformAdApi && this.platformAdApi.createVideo();
    }
    /**展示激励视频 */
    public showVideo(_type: string, _successCallback?: Function, _failCallback?: Function) {

        //声音控制
        cc.audioEngine.pauseAll()

        this.platformAdApi && this.platformAdApi.showVideo(_type, () => {
            _successCallback && _successCallback();
            cc.audioEngine.resumeAll()
        }, () => {
            _failCallback && _failCallback();
            cc.audioEngine.resumeAll()
        });
    }
    public showVideoAward() {
        this.platformAdApi && this.platformAdApi.showVideoAward();
    }
    public showVideoFail() {
        this.platformAdApi && this.platformAdApi.showVideoFail();
    }
    /**创建插屏 */
    public createInsertAd() {
        this.platformAdApi && this.platformAdApi.createInsertAd();
    }
    /**展示插屏 */
    public showInsertAd(adIdKey, openIdKey?: string) {
        if (!this.adMGBackend.adShow(openIdKey)) {
            return;
        }
        this.platformAdApi && this.platformAdApi.showInsertAd(adIdKey, openIdKey);
    }
    /**创建原生 */
    public createNativeAd() {
        this.platformAdApi && this.platformAdApi.createNativeAd();
    }
    /**展示原生 */
    public showNativeAd(adIdKey: string, _callback: Function, openIdKey?: string) {
        if (!this.adMGBackend.adShow(openIdKey)) {
            return;
        }
        this.platformAdApi && this.platformAdApi.showNativeAd(adIdKey, _callback, openIdKey);
    }
    /**原生被点击 */
    public onNativeAdClick(_id: string) {
        this.platformAdApi && this.platformAdApi.onNativeAdClick(_id);
    }
    /**原生展示 */
    public onNativeReportAdShow(_id: string) {
        this.platformAdApi && this.platformAdApi.onNativeReportAdShow(_id);
    }
    /**创建原生icon */
    public createNativeIconAd() {
        this.platformAdApi && this.platformAdApi.createNativeIconAd();
    }
    /**展示原生icon */
    public showNativeIconAd() {
        this.platformAdApi && this.platformAdApi.showNativeIconAd();
    }
    /**原生icon被点击 */
    public onNativeIconAdClick(_id: string) {
        this.platformAdApi && this.platformAdApi.onNativeIconAdClick(_id);
    }
    /**创建格子广告组件 */
    public createGridAd(adIdKey: string, openIdKey?: string) {
        this.platformAdApi && this.platformAdApi.createGridAd(adIdKey, openIdKey);
    }
    /**添加icon到桌面 */
    public addDesktop(_callback: Function) {
        this.platformAdApi && this.platformAdApi.addDesktop(_callback);
    }
    /**是否已经创建桌面图标 */
    public hasShortcutInstalled(_callback: Function) {
        this.platformAdApi && this.platformAdApi.hasShortcutInstalled(_callback);
    }
    /**分享录屏 */
    public onShareVideo(_callback: Function) {
        this.platformAdApi && this.platformAdApi.onShareVideo(_callback);
    }
    /**开始录制视频 */
    public startRecordScreen() {
        this.platformAdApi && this.platformAdApi.startRecordScreen();
    }
    /**结束录制视频 */
    public stopRecordScreen() {
        this.platformAdApi && this.platformAdApi.stopRecordScreen();
    }
    /**创建更多游戏按钮 */
    public createMoreGamesBtn() {
        this.platformAdApi && this.platformAdApi.createMoreGamesBtn();
    }
    /**展示更多游戏按钮 */
    public showMoreGamesBtn() {
        this.platformAdApi && this.platformAdApi.showMoreGamesBtn();
    }
    /**
     * 跳转游戏
     * @param _packageName 包名
     */
    public jumpToGame(_packageName: string) {
        this.platformAdApi && this.platformAdApi.jumpToGame(_packageName);
    }
    /** 添加彩签*/
    public addColorBookmark(_callback: Function) {
        this.platformAdApi && this.platformAdApi.addColorBookmark(_callback);
    }
    /**订阅app */
    public addSubscribeApp(_callback: Function) {
        this.platformAdApi && this.platformAdApi.addSubscribeApp(_callback);
    }

    /**震动==>短震 */
    public vibrateShort() {
        this.platformAdApi && this.platformAdApi.vibrateShort();
    }
    /**震动==>长震 */
    public vibrateLong() {
        this.platformAdApi && this.platformAdApi.vibrateLong();
    }

}
